using System;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;

namespace Tools
{
    public class FileReaderHelper
    {
        private static readonly FileReaderHelper Instance = new();

        private FileReaderHelper()
        {
            CreateResourceManager();
        }

        public static FileReaderHelper GetInstance()
        {
            return Instance;
        }

        private static OpenHarmonyJSObject _resourceManager;

        private static void CreateResourceManager()
        {
            var resourceManagerClass = new OpenHarmonyJSClass("MyResourceManager");
            _resourceManager = resourceManagerClass.CallStatic<OpenHarmonyJSObject>("getInstance");
            Debug.Log($"CreateResourceManager:resourceManagerObj={_resourceManager}");
        }

        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="path">文件相对于rawfile的路径</param>
        /// <returns>文件内容</returns>
        public byte[] getFileContentBase64(string path)
        {
            Debug.Log($">>>getFileContentBase64:  {path} begin _resourceManager={_resourceManager}");
            var content = _resourceManager.Call<string>("getFileContentBase64", path);
            Debug.Log($">>>getFileContentBase64: {path}, file content: {content.Length}");
            var byteDataArr = System.Convert.FromBase64String(content);
            Debug.Log($">>>getFileContentBase64: after FromBase64String. byteDataArr.Length={byteDataArr.Length}");
            return byteDataArr;
        }

        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="path">文件相对于rawfile的路径</param>
        /// <returns>文件内容</returns>
        public string getFileContent(string path)
        {
            Debug.Log($">>>getFileContentArray:  {path} begin _resourceManager={_resourceManager}");
            var content = _resourceManager.Call<string>("getFileContent", path);
            Debug.Log($">>>getFileContent: ={content}");
            return content;
        }
    }
}